package com.example.music;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MusicPlayActivity extends AppCompatActivity implements View.OnClickListener{
    //UI组件定义
    ImageView img_cd_logo,img_loop,img_pre,img_play,img_next,img_share;
    TextView txt_time_begin,txt_time_end;
    SeekBar seekBar;
    int degree=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_play);
        //初始化UI组件对象
        initViews();
        //初始化音乐资源并开始播放音乐
        initMedia();
        //启动线程
        new MyThread().execute();
    }

    //初始化UI组件对象
    private void initViews() {
        img_cd_logo=this.findViewById(R.id.img_cd_logo);
        img_loop=this.findViewById(R.id.img_loop);
        img_next=this.findViewById(R.id.img_next);
        img_pre=this.findViewById(R.id.img_pre);
        img_play=this.findViewById(R.id.img_play);
        img_share=this.findViewById(R.id.img_share);
        txt_time_begin=this.findViewById(R.id.text_time_begin);
        txt_time_end=this.findViewById(R.id.text_time_end);
        seekBar=this.findViewById(R.id.seekBar);
        //给五个功能按钮添加单击事件
        img_loop.setOnClickListener(this);
        img_pre.setOnClickListener(this);
        img_play.setOnClickListener(this);
        img_next.setOnClickListener(this);
        img_share.setOnClickListener(this);
        //设置MediaPlay播放完毕后的监听
        MusicData.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                MusicData.isRun=false;
                MusicData.flag=0;
            }
        });
        //设置SeekBar拖动的进度监听
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if(MusicData.mediaPlayer!=null && MusicData.mediaPlayer.isPlaying()){
                    //获取当前SeekBar拖动后的进度值
                    int position=seekBar.getProgress();
                    //修改MediaPlayer的播放进度
                    MusicData.mediaPlayer.seekTo(position);
                }
            }
        });
    }
    //重启线程
    private void restart(){
        if(!MusicData.isRun){
            MusicData.isRun=true;
            MusicData.flag=1;
        }
        new MyThread().execute();
    }
    //图片按钮的单击事件响应
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_loop:
                //设置播放循环模式
                break;
            case R.id.img_pre:
                //切换上一首音乐
                MusicData.index--;
                if(MusicData.index<0){
                    MusicData.index=0;
                }
                initMedia();
                this.restart();
                break;
            case R.id.img_play:
                //暂停和播放音乐
                if(MusicData.flag==1){
                    //如果是播放状态，则切换到暂停
                    MusicData.flag=2;
                    img_play.setImageResource(android.R.drawable.ic_media_pause);
                    if(MusicData.mediaPlayer!=null&&MusicData.mediaPlayer.isPlaying()){
                        MusicData.mediaPlayer.pause();
                    }
                }else{
                    //如果是非播放状态，切换到播放状态
                    MusicData.flag=1;
                    img_play.setImageResource(android.R.drawable.ic_media_play);
                    if(MusicData.mediaPlayer!=null){
                        MusicData.mediaPlayer.start();
                    }
                }
                this.restart();
                break;
            case R.id.img_next:
                //切换下一首音乐
                MusicData.index++;
                if(MusicData.index>=MusicData.data.size()){
                    MusicData.index=MusicData.data.size()-1;
                }
                initMedia();
                this.restart();
                break;
            case R.id.img_share:
                //分享给好友
                break;
        }
    }
    //格式化显示播放时间
    private String showTime(long time){
        SimpleDateFormat sdf=new SimpleDateFormat("mm:ss");
        return sdf.format(new Date(time));
    }
    //重写返回按钮事件
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        MusicData.stopMedia();
    }
    //子线程控制音乐播放和显示对应效果
    class MyThread extends AsyncTask<Void,Integer,Void>{
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            //旋转cd图片
            img_cd_logo.setRotation(values[0]);
            //改变进度条的值
            seekBar.setProgress(MusicData.mediaPlayer.getCurrentPosition());
            txt_time_begin.setText(showTime(MusicData.mediaPlayer.getCurrentPosition()));
        }

        @Override
        protected Void doInBackground(Void... voids) {
            while (MusicData.isRun){
                if(MusicData.flag==1){
                    //旋转cd图片
                    degree+=1;
                    publishProgress(degree);
                    if(degree>360){
                        degree=0;
                    }
                }
                //线程休眠
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }

    //初始化SeekBar的值
    private void  initSeekBar(){
        int max = MusicData.mediaPlayer.getDuration();
        //设置最大值
        seekBar.setMax(max);
        txt_time_end.setText(showTime(max));
        //设置进度图的当前值
        seekBar.setProgress(0);
        txt_time_begin.setText(0+"");
    }

    //音乐播放初始化
    public void initMedia(){
        //如果音乐播放对象为空，则新建一个对象
        if(MusicData.mediaPlayer==null){
            MusicData.mediaPlayer=new MediaPlayer();
        }
        //如果有正在播放的资源，则停止
        if(MusicData.mediaPlayer.isPlaying()){
            MusicData.mediaPlayer.stop();
            MusicData.mediaPlayer.reset();
        }
        //设置当前要播放的音乐资源
        String path=MusicData.data.get(MusicData.index).get("path");
        try {
            MusicData.mediaPlayer.reset();
            MusicData.mediaPlayer.setDataSource(path);
            //初始化音乐资源
            MusicData.mediaPlayer.prepare();
            //开始播放音乐
            MusicData.mediaPlayer.start();
            //设置SeekBar的初始值
            initSeekBar();
            //初始化播放的状态
            MusicData.isRun=true;
            MusicData.flag=1;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}